using System;
using System.Globalization;
using System.Windows.Data;

namespace VisualWip.PresentationModel
{
    public abstract class AgeConverterBase : IValueConverter
    {
        private readonly Func<DateTime> _now;

        protected AgeConverterBase()
            : this(() => DateTime.Now)
        {
        }

        protected AgeConverterBase(Func<DateTime> now)
        {
            _now = now;
        }

        public DateTime Now
        {
            get { return _now.Invoke(); }
        }

        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            DateTime date;
            int parameterValue;
            try
            {
                date = (DateTime) value;
                parameterValue = int.Parse((string) parameter);
            }
            catch (Exception)
            {
                return null;
            }

            return Calculate(date, parameterValue);
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion

        protected abstract bool Calculate(DateTime date, int value);
    }
}